package threadTest.collectionTest;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
 * 功能描述: map 测试 - 并发编程测试
 * 2024/02/22
 * <p>
 * ZhangYi
 */
public class MapTest {
    public static void main(String[] args) {
        //HashMap<String, String> map = new HashMap<>();  //线程不安全

        // 线程安全
        //Map<String, String> map = Collections.synchronizedMap(new HashMap<>());

        Map<String, String> map = new ConcurrentHashMap<>();

        for (int i = 0; i < 300000; i++) {
            final int index = i;
            new Thread(()->{
                map.put("key"+index,"value"+index);
            },String.valueOf(i)).start();
        }
        System.out.println(map.size());

    }
}
